package com.xzx.controller;

import com.xzx.dto.CommonPage;
import com.xzx.dto.SuccessResponseDTO;
import com.xzx.po.PermissionPO;
import com.xzx.po.RolePO;
import com.xzx.service.PermissionService;
import com.xzx.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author xinzhixuan
 * @version 1.0
 * @date 2025/8/18 14:08
 */
@Slf4j
@RequestMapping("/permission")
@Controller
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/list")
    public String list(Model model) {
        List<PermissionPO> tree = permissionService.treeAll();
        model.addAttribute("tree", tree);
        return "permission/list";
    }

    @ResponseBody
    @PostMapping("/doAdd")
    public SuccessResponseDTO doAdd(@RequestBody PermissionPO permissionPO) {
        log.info("doAdd permissionPO:{}", permissionPO);
        permissionService.doAdd(permissionPO);
        return SuccessResponseDTO.of();
    }

    @ResponseBody
    @PostMapping("/doDelete")
    public SuccessResponseDTO doDelete(@RequestParam("id") Long id) {
        log.info("doDelete id:{}", id);
        permissionService.delete(id);
        return SuccessResponseDTO.of();
    }

    @GetMapping("/edit")
    public String edit(@RequestParam("id") Long id, Model model) {
        PermissionPO permissionPO = permissionService.getById(id);
        model.addAttribute("permission", permissionPO);
        return "permission/edit";
    }

    @ResponseBody
    @PostMapping("/doEdit")
    public SuccessResponseDTO doEdit(@RequestBody PermissionPO permissionPO) {
        log.info("doEdit permissionPO:{}", permissionPO);
        permissionService.doEdit(permissionPO);
        return SuccessResponseDTO.of();
    }

}
